package com.waimai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.Dto.DishDto;
import com.waimai.common.R;
import com.waimai.domain.Dish;
import com.waimai.service.DishFlavorService;
import com.waimai.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    //分页查询 与员工表基本一致；
    public R<Page> page(int page,int pageSize,String name){
        Page page1 =new Page(page,pageSize);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name !=null,Dish::getName,name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(page1,lambdaQueryWrapper);
        return R.success(page1);
    }

    //添加方法
    @PostMapping
     public R<String> save(@RequestBody DishDto dishDto){
        log.info("当前值为{}",dishDto);
        dishService.saveWithFlavor(dishDto);
        return R.success("1");

     }

     @GetMapping("/{id}")
     public R<DishDto> get(@PathVariable Long id){
         DishDto withFlavor = dishService.getWithFlavor(id);
         return R.success(withFlavor);
     }
     //修改提交
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("当前值为{}",dishDto);
        dishService.updataWithFlavor(dishDto);
        return R.success("1");

    }
    @GetMapping("/list")
    public R<List<Dish>>  list(Dish dish){
        //创造构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper =new LambdaQueryWrapper<>();
        //设置构造器
        dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //把停售的过滤掉
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        //排序
        dishLambdaQueryWrapper.orderByDesc(Dish::getSort);

        List<Dish> list = dishService.list(dishLambdaQueryWrapper);

        return  R.success(list);
    }


}
